package com.scholarship.modules.backend.service;

import com.scholarship.common.utils.CommonUtil;
import com.scholarship.modules.backend.dao.Teacher;
import com.scholarship.modules.backend.entity.RewardTable;
import com.scholarship.modules.backend.entity.TeacherAuthority;
import com.scholarship.modules.backend.mapper.TeacherAuthorityMapper;
import com.scholarship.modules.login.entity.Role;
import com.scholarship.modules.login.entity.User;
import com.scholarship.modules.login.mapper.LRoleMapper;
import com.scholarship.modules.login.mapper.LUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author zhz
 * @date 2021/7/30 21:43
 * 备注：
 */
@Service
public class TeacherAuthorityService {
    @Autowired
    TeacherAuthorityMapper teacherAuthorityMapper;
    @Autowired
    RewardTableService rewardTableService;
    @Autowired
    LUserMapper lUserMapper;
    @Autowired
    LRoleMapper lRoleMapper;

    //判断教师是否有操作该表id的权限
    public Boolean isTeacherAuthority(String userId,Integer tableId){
        //从第0为开始记权限
        tableId-=1;
        //判断教师是否有该表权限
        if((1<<tableId)<teacherAuthorityMapper.getTableAuthority(userId) && ((teacherAuthorityMapper.getTableAuthority(userId) >> tableId)&1)==1) return true;
        return false;
    }

    //赋予老师，修改表的权限
    public Boolean setTableAuthority(String userId,List<Integer> tableIds){
        int tableAuthority=0;
        try{
            for(Integer tableId:tableIds){
                tableId-=1;
                int num = 1<<tableId;
                tableAuthority = tableAuthority|num;
            }
            teacherAuthorityMapper.setTableAuthority(userId,tableAuthority);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //创建教师账号
    public Boolean creatTeacher(Teacher teacher){
        try{
            String userId = teacher.getUserId();
            //账号信息
            User user = new User();
            user.setUserId(userId);  //学号
            user.setName(teacher.getName());  //姓名
            user.setState(1);
            //密码进行MD5盐值加密
            String new_password = CommonUtil.encryptPassword(userId,userId);
            user.setPassword(new_password); //密码

            //身份信息
            Role role = new Role();
            role.setUserId(teacher.getUserId());  //学号
            role.setRole(2);    //身份:2教师

            //判断分配账号是否失败
            if(lUserMapper.insertUser(user)==0||lRoleMapper.insertRole(role)==0)return false;
            int tableAuthority=0;
            for(Integer tableId:teacher.getTableIds()){
                tableId-=1;
                int num = 1<<tableId;
                tableAuthority = tableAuthority|num;
            }
            TeacherAuthority teacherAuthority = new TeacherAuthority();
            teacherAuthority.setUserId(teacher.getUserId());
            teacherAuthority.setClassAuthority(teacher.getClassAuthority());
            teacherAuthority.setTableAuthority(tableAuthority);

            teacherAuthorityMapper.insertTableAuthority(teacherAuthority);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //列出所有教师账号
    public List<TeacherAuthority>  listAllTeachers(){
        List<TeacherAuthority> teachers=teacherAuthorityMapper.listAllTeachers();
        List<RewardTable> listRewardTable = rewardTableService.listRewardTable();
        for(TeacherAuthority teacher:teachers){
            LinkedHashMap<Integer,String> tableIds = new LinkedHashMap<>();
            int table_authority = teacher.getTableAuthority();
            for(RewardTable rewardTable:listRewardTable){
                if((1<<rewardTable.getId())<table_authority&&(table_authority>>(rewardTable.getId()-1)&1)==1)
                    tableIds.put(rewardTable.getId(),rewardTable.getName());
            }
            teacher.setTableIds(tableIds);
        }
        return teachers;
    }

    /**
     * 修改老师账号密码
     * @param userId 老师id
     * @param newPwd 新密码
     * @return 成功true，失败false
     * @throws Exception
     */
    public Boolean changeTeacherPwd(String userId, String newPwd) throws Exception {
        Role role = lRoleMapper.getRoleByUserId(userId);
        if(role.getRole()==2){
            String saltPwd = CommonUtil.encryptPassword(newPwd,userId);
            lUserMapper.updatePasswordByUserId(userId,saltPwd);
            return true;
        }
        return false;
    }

    //删除教师账号
    @Transactional(rollbackFor=Exception.class)//事务的注解，失败就rollback
    public Boolean deleteTeacher(String userId) throws Exception {
        if(teacherAuthorityMapper.deleteTeacher(userId)!=0)
            return true;
        else
            return false;
    }
}
